package com.elder.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.elder.business.domain.ElderlyInfoDTO;
import com.elder.business.service.IElderlyApartmentBedsService;
import com.elder.common.annotation.Anonymous;
import com.elder.common.utils.LogUtils;
import com.elder.common.utils.minio.MinioService;
import com.elder.common.utils.uuid.UUID;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.elder.common.annotation.Log;
import com.elder.common.core.controller.BaseController;
import com.elder.common.core.domain.AjaxResult;
import com.elder.common.enums.BusinessType;
import com.elder.business.domain.ElderlyInfo;
import com.elder.business.service.IElderlyInfoService;
import com.elder.common.utils.poi.ExcelUtil;
import com.elder.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 老人信息Controller
 * 
 * @author elder
 * @date 2023-11-26
 */
@RestController
@RequestMapping("/business/elderInfo")
public class ElderlyInfoController extends BaseController
{
    @Autowired
    private IElderlyInfoService elderlyInfoService;
    @Autowired
    private MinioService minioService;
    @Autowired
    private IElderlyApartmentBedsService elderlyApartmentBedsService;

    /**
     * 查询老人信息列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(String elderName,  Integer pageNum,Integer pageSize)
    {
        return elderlyInfoService.selectElderlyInfoList(elderName,pageNum,pageSize);
    }

    /**
     * 导出老人信息列表
     */

    @Anonymous
    @Log(title = "老人信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElderlyInfo elderlyInfo)
    {
        List<ElderlyInfo> list = elderlyInfoService.selectElderlyInfoExportList(elderlyInfo);
        ExcelUtil<ElderlyInfo> util = new ExcelUtil<ElderlyInfo>(ElderlyInfo.class);
        util.exportExcel(response, list, "老人信息数据");
    }

    /**
     * 获取老人信息详细信息
     */
    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(elderlyInfoService.selectElderlyInfoById(id));
    }

    /**
     * 新增老人信息
     */
    @Anonymous
    @PostMapping
    public AjaxResult add(@RequestBody ElderlyInfoDTO elderlyInfo)
    {
        return toAjax(elderlyInfoService.insertElderlyInfo(elderlyInfo));
    }

    /**
     * 修改老人信息
     */
    @Anonymous
    @PutMapping
    public AjaxResult edit(@RequestBody ElderlyInfoDTO elderlyInfo)
    {
        return toAjax(elderlyInfoService.updateElderlyInfo(elderlyInfo));
    }

    /**
     * 删除老人信息
     */
    @Anonymous
    @Log(title = "老人信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(elderlyInfoService.deleteElderlyInfoByIds(ids));
    }
    @Anonymous
    @PostMapping("/upload")
    public String minioUploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        try {
            System.out.println("文件正在上传");
            String fileName = file.getOriginalFilename();
            String uuid = UUID.randomUUID().toString();
            String imgType = fileName.substring(fileName.lastIndexOf("."));
            String finalFileName = uuid + imgType;
            JSONObject object = minioService.uploadOneFile(finalFileName, file);
            String previewUrl = object.getString("previewUrl");
            logger.info("elder info upload sucess!!!",previewUrl);
            return previewUrl;
        } catch (Exception e) {
            logger.error("elder info upload error :{}",e);
            return JSON.toJSONString(AjaxResult.error(e.getMessage()));
        }
    }

    @Anonymous
    @RequestMapping("/delFile")
    public AjaxResult delFile(String fileName){
        String s = minioService.delOneFile(fileName);
        return AjaxResult.success(s);
    }

    @Anonymous
    @RequestMapping("/allElderList")
    public TableDataInfo ElderAlllist()
    {
        List<JSONObject> list = elderlyInfoService.selectElderlyAllList();
        return getDataTable(list);
    }

    /***
     * @description 获取老人档案
     * @return TableDataInfo
     * @author zyk
     * @date 2023/12/3 17:32
     */
    @Anonymous
    @RequestMapping("/getElderFiles ")
    public TableDataInfo Elder(String elderName){
        List<JSONObject> list = elderlyInfoService.getElderlyFiles(elderName);
        return getDataTable(list);
    }

    /***
     * @description 住户床位调整
     * @param elderId
     * @param roomId
     * @param bedCode
     * @return AjaxResult
     * @author zyk
     * @date 2023/12/11 14:29
     */
    @Anonymous
    @RequestMapping("/adjustBed")
    public AjaxResult adjustElderBed(Long elderId , Long roomId, String bedCode){
        elderlyApartmentBedsService.adjustElderBedInfo(elderId,roomId,bedCode);
        return AjaxResult.success();
    }

    /***
     * @description 获取今天加入的老人信息
     * @return JSONObject
     * @author zyk
     * @date 2024/2/29 17:02
     */
    @Anonymous
    @RequestMapping("/newElderInfo")
    public JSONObject getTodayJoinElderInfo(){
        return elderlyInfoService.getTodayJoinElderInfo();
    }

    /***
     * @description 根据姓名获取老人信息
     * @param elderName
     * @return JSONObject
     * @author zyk
     * @date 2024/3/2 20:55
     */
    @Anonymous
    @RequestMapping("/getElderbyName")
    public JSONObject getElder(String elderName){
        return elderlyInfoService.getElderInfoByName(elderName);
    }
}
